//
//  CZcomeposeButton.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/8.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit
let btncount = 5

class CZcomeposeButton: UITabBar {
    
    
    var  composeclouse :(()->())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.frame.size.width / CGFloat(btncount)
//        print(frame.width)
        var index: CGFloat = 0
        let height = self.frame.size.height
        
        for item in subviews {
            //筛选是tabbarbutton的按钮  其余的view 不要
            if( item .isKindOfClass(NSClassFromString("UITabBarButton")!)){
            item.frame = CGRect(x: index * width, y: 0, width: width, height: height)
            
            index++
            if index == 2 {
                index++
            }
            
        }
        
        }
        composeButton.frame = (frame: CGRect(x: 2*width, y: 0, width: width, height: height))
        
        self.addSubview(composeButton)//添加到tabbar
        
    }
    
    
    //闭包准备传值
    //oc方法
  @objc  func btnDidClick(){
    composeclouse!()
    }
    

    //懒加载中间加号
  private  lazy var composeButton: UIButton = {
    
        let button = UIButton()
        button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        button.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
       
//        button.addTarget(self, action: #selector(CZcomeposeButton.btnDidClick), forControlEvents: UIControlEvents.TouchUpInside)
    button.addTarget(self, action: #selector(CZcomeposeButton.btnDidClick), forControlEvents: .TouchUpInside)//添加点击事件
    
    
        return button
    }()
    
}
